package m07;

import java.io.Serializable;

/**
 * This class controls the Ship.
 * 
 * @author Jessica Roberts, Eric Hamiltion, Joshua Clark, Samantha Rich
 * @version 3.1 19 November 2012
 */

public class Ship implements Serializable{
	private static final long serialVersionUID = -829734334437145046L;
	private String name;
	private int size;
	private Good[] cargo;
	private int fuel;

	/**
	 * This constructor takes in the parameters for the Ship and sets 
	 * them equal to the instantiated variables.
	 * 
	 * @param name The name of the ship
	 * @param size The maximum capacity of the ship
	 */
	public Ship (String name, int size)
	{
		this.name = name;
		this.size = size;
		cargo = new Good[size];
		fuel = 10;
	} // end constructor

	/**
	 * This method returns the name of the Ship.
	 * 
	 * @return The name of the Ship.
	 */
	public String getName()
	{
		return name;
	} // end method getName

	/**
	 * This method returns the number of cargo bays used.
	 * 
	 * @return The number of cargo bays used.
	 */
	public int getSpaceUsed()
	{
		int count = 0;
		for(int i = 0; i<size; i++)
		{
			if(getCargo()[i]!=null)
				count++;
		} // end for loop
		return count;
	} // end method getSpaceUsed
	
	/**
	 * This method returns the number of bays filled with a particular good
	 * 
	 * @param name The name of the good
	 * @return A string representing the number of cargo bays filled with a particular good
	 */
	public String getGoodAmount(String name)
	{
		int count = 0;
		if(getSpaceUsed()>0)
		{
			for(int i = 0; i < getSpaceUsed(); i++)
			{	
				if(cargo[i].getName().equalsIgnoreCase(name))
					count++;
			} // end for loop
		} // end if statement
		return ""+count;
	} // end method getGoodAmount(String)

	/**
	 * This method adds cargo to the Ship.
	 * 
	 * @param good The good to be added to the Ship
	 * @return boolean: true means it removed, false means there's no room
	 */
	public boolean addCargo(Good good)	{
		if (getSpaceUsed() < size){
			getCargo()[getSpaceUsed()] = good;
			return true;
		}
		return false;
	} // end method addCargo(Good)

	/**
	 * This method removes cargo from the Ship
	 * @param name The name of the good to be removed from the ship
	 */
	public void removeCargo(String name) {
		int indexRemoved = -1;
		int size = getSpaceUsed();
		for(int i = 0; i < getSpaceUsed(); i++) {
			if (cargo[i] != null){
				if (cargo[i].getName().equalsIgnoreCase(name)){
					if (indexRemoved == -1) {
						indexRemoved = i;
					} // end if statement
				} // end if statement
			} // end if statement
		} // end for loop
		if (indexRemoved != -1){
			cargo[indexRemoved] = cargo[size - 1];
			cargo[size - 1] = null;
		} // end else if
	} // end method removeCargo(String)

	/**
	 * check if a ship contains the inputed good
	 * @param name the name of the good
	 * @return a boolean representing if the good is present
	 */
	public boolean contains(String name)
	{
		boolean contained = false;
		for(int i = 0; i < getSpaceUsed(); i++)
		{
			if (cargo[i] != null){
				if (cargo[i].getName().equalsIgnoreCase(name))
				{
					contained = true;
				} // end if statement
			}
		} // end for loop
		return contained;
	} // end method contains(String)

	/**
	 * converts from a string to a good object
	 * @param goodName the string to convert from
	 * @return the good to convert to.
	 */
	public Good goodConverter(String goodName){
		Good goodToAdd;
		if (goodName.equalsIgnoreCase("water")){
			goodToAdd= new Good("Water", 0, 0, 30, 3, 4, "LOTSOFWATER", "DESERT", 30, 50, 0);
		} // end if statement
		else if (goodName.equalsIgnoreCase("furs")){
			goodToAdd= new Good("Furs", 0, 0, 250, 10, 10, "RICHFAUA", "LIFELESS", 230, 280, 0);
		} // end else if
		else if (goodName.equalsIgnoreCase("food")){
			goodToAdd= new Good("Food", 1, 0, 100, 5, 5, "RICHSOIL", "POORSOIL", 90, 160, 0);
		} // end else if
		else if (goodName.equalsIgnoreCase("ore")){
			goodToAdd= new Good("Ore", 2, 2, 350, 20, 10, "MINERALRICH", "MINERALPOOR", 350, 420, 0);
		} // end else if
		else if (goodName.equalsIgnoreCase("games")){
			goodToAdd= new Good("Games", 3, 1, 250, -10, 5, "ARTISTIC", null, 160, 270, 0);
		} // end else if
		else if (goodName.equalsIgnoreCase("firearms")){
			goodToAdd= new Good("Firearms", 3, 1, 1250, -75, 100, "WARLIKE", null, 600, 1100, 0);
		} // end else if
		else if (goodName.equalsIgnoreCase("medicine")){
			goodToAdd= new Good("Medicine", 4, 1, 650, -20, 10, "LOTSOFHERBS", null, 400, 700, 0);
		} // end else if
		else if (goodName.equalsIgnoreCase("machines")){
			goodToAdd= new Good("Machines", 4, 3, 900, -30, 5, null, null, 600, 800, 0);
		} // end else if
		else if (goodName.equalsIgnoreCase("narcotics")){
			goodToAdd= new Good("Narcotics", 5, 0, 3500, -125, 150, "WEIRDMUSHROOMS", null, 2000, 3000, 0);
		} // end else if
		else{
			goodToAdd= new Good("Robots", 6, 4, 5000, -150, 100, null, null, 3500, 5000, 0);
		} // end else
		return goodToAdd;
	} // end method goodConverter(String)

	/**
	 * a getter for the fuel
	 * @return the amount of fuel
	 */
	public int getFuel() {
		return fuel;
	} // end method getFuel
	
	/**
	 * an actual setter for the fuel amount
	 * @param fuelAmt
	 */
	public void setFuel(int fuelAmt){
		fuel = fuelAmt;
	} // end method setFuel(int)
	
	/**
	 * a setter for the fuel amount
	 * @param dist the distance traveled.
	 */
	public void setFuelFromDist(int dist) {
		fuel = fuel - dist;
	} // end method setFuelFromDist(int)

	/**
	 * a getter for the cargo array
	 * @return cargo the array of cargo objects
	 */
	public Good[] getCargo() {
		return cargo;
	} // end method getCargo

} // end class